package erfgame.frontend.desktop.view.terrain.iso.interaction;

import java.awt.Point;

import erfgame.core.Point3D;
import erfgame.core.entity.Entity;
import erfgame.core.world.World;
import erfgame.core.world.WorldContext;
import erfgame.core.world.WorldDAOManager;
import erfgame.core.world.terrain.BlockSurfaceImageFactoryManager;
import erfgame.core.world.terrain.MaterialType;
import erfgame.core.world.terrain.TerrainUpdate;
import erfgame.core.world.terrain.TerrainUpdateByMaterialTypeFactory;
import erfgame.core.world.terrain.TerrainUpdateGroup;
import erfgame.core.world.terrain.TerrainUtils;

import erfgame.frontend.desktop.view.terrain.iso.RotationContext;

public class TerrainUpdateInteractionFactory 
	implements EntityInteractionFactory {
	
	private WorldDAOManager worldDAOManager;
	private BlockSurfaceImageFactoryManager bsiFactoryManager;
	private TerrainUpdateByMaterialTypeFactory terrainUpdateFactory;
	private MaterialType materialType;
	private WorldContext worldContext;
	private RotationContext rotationContext;
	private TerrainUpdatePositionFactory positionFactory;
	
	public TerrainUpdateInteractionFactory( 
			WorldDAOManager worldDAOManager,
			BlockSurfaceImageFactoryManager bsiFactoryManager,
			TerrainUpdateByMaterialTypeFactory terrainUpdateFactory, 
			MaterialType materialType, 
			WorldContext worldContext,
			RotationContext rotationContext,
			TerrainUpdatePositionFactory positionFactory
	) {
		this.worldDAOManager = worldDAOManager;
		this.bsiFactoryManager = bsiFactoryManager;
		this.terrainUpdateFactory = terrainUpdateFactory;
		this.materialType = materialType;
		this.worldContext = worldContext;
		this.rotationContext = rotationContext;
		this.positionFactory = positionFactory;
	}

	public Interaction createInteraction(Entity entity, Point3D terrain) {
		World world = this.worldContext.getCurrentWorld();
		
		TerrainUpdateGroup terrainUpdate;
		terrainUpdate = this.terrainUpdateFactory.createTerrainUpdate((byte)materialType.getId());
		
		Point3D position = this.positionFactory.getPosition(
				terrain, 
				terrainUpdate, 
				world
		);
		if( position != null ) {
			return new TerrainUpdateInteraction(
					world, 
					position, 
					rotationContext.getCurrentRotation(),
					terrainUpdate, 
					worldDAOManager, 
					bsiFactoryManager
			);
		} else {
			return null;
		}
	}
}
